package com.iwdnb.gkgz.infrastructure.dao.repo.impl;

import java.util.List;
import java.util.Objects;

import com.iwdnb.gkgz.infrastructure.dao.entity.StockPoolDO;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockPoolRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STOCK_POOL;

@Component
public class StockPoolRepositoryImpl implements StockPoolRepository {
    @Autowired
    private MetaDataDAO metaDataDAO;

    @Override
    public StockPoolDO findByCode(String code) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", code);
        return metaDataDAO.get(OBJECT_CODE_STOCK_POOL, metaQuery, StockPoolDO.class);
    }

    @Override
    public void deleteByCode(String code) {
        StockPoolDO stockPoolDO = findByCode(code);
        if (Objects.nonNull(stockPoolDO)) {
            metaDataDAO.delete(OBJECT_CODE_STOCK_POOL, stockPoolDO.getId());
        }
    }

    @Override
    public List<StockPoolDO> findAll() {
        return metaDataDAO.queryListByCondition(OBJECT_CODE_STOCK_POOL,new MetaQuery(),StockPoolDO.class);
    }

    @Override
    public void save(StockPoolDO stockPoolDO) {
        if (Objects.isNull(stockPoolDO.getId())) {
            metaDataDAO.insert(OBJECT_CODE_STOCK_POOL, stockPoolDO);
        } else {
            metaDataDAO.update(OBJECT_CODE_STOCK_POOL, stockPoolDO);
        }
    }
}
